package cn.ultrarisk.scadmin.manager.web.controller.log;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.scadmin.manager.biz.common.dao.OperationLogDAO;
import cn.ultrarisk.scadmin.manager.biz.common.entity.OperationLog;
import cn.ultrarisk.scadmin.manager.biz.user.dao.AdminDAO;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Admin;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.log.OperationLogDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * OperateController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/log/operation")
public class OperationLogController extends BaseController {
    @Autowired
    private OperationLogDAO operationLogDAO;
    @Autowired
    private AdminDAO adminDAO;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Map<String, Object> list(OperationLogDTO dto, int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();

        Page<OperationLog> paged = operationLogDAO.findPage(dto.getOptStartTime(), dto.getOptEndTime(), dto.getIsScheduler(), pageNo, pageSize);
        map.put("paged", paged);

        List<Integer> adminIds = new ArrayList<Integer>();
        for (OperationLog operationLog : paged.getList()) {
            if (operationLog.getAdminId() != null) {
                if (operationLog.getAdminId() > 0) {
                    adminIds.add(operationLog.getAdminId());
                }
            }
        }
        if (adminIds.size() > 0) {
            List<Admin> admins = adminDAO.findByIds(adminIds);
            Map<Integer, Admin> adminMap = new HashMap<Integer, Admin>();
            for (Admin admin : admins) {
                adminMap.put(admin.getId(), admin);
            }
            map.put("admins", adminMap);
        }
        return map;
    }
}
